﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_maintain.TNS_Violation
{
    public class TNS_Violations : DapperRepository<TodoAppDbContext>, ITransientDependency, ITNS_Violations
    {
        public TNS_Violations(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 油费显示
        public async Task<PageResponse<TNS_ViolationDto>> GetVio(string Viotitel, string VioNumber, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TNS_ViolationDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_ViolationModel where 1=1 ";
            if (!string.IsNullOrEmpty(Viotitel))
            {
                sql += $" and Viotitel like '%{Viotitel}%'";
            }
            if (!string.IsNullOrEmpty(VioNumber))
            {
                sql += $" and VioNumber  like '%{VioNumber}%'";
            }

            var list = await dbConnection.QueryAsync<TNS_ViolationDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TNS_ViolationDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 油费添加
        public async Task<int> VioAdds(TNS_ViolationDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_ViolationModel values ('{info.Id}','{info.Viotitel}','{info.VioNumber}','{info.Vioyuan}','{info.VioPeop}','{info.Viomiao}','{info.VioTim}','{info.VioTxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 油费删除
        public async Task<int> DeletVio(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_ViolationModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

        #region 油费修改
        public async Task<int> UpdateVioup(TNS_ViolationDto tMS_Induction)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_ViolationModel set Viotitel='{tMS_Induction.Viotitel}',VioNumber='{tMS_Induction.VioNumber}',Vioyuan='{tMS_Induction.Vioyuan}',VioPeop='{tMS_Induction.VioPeop}',Viomiao='{tMS_Induction.Viomiao}',VioTim='{tMS_Induction.VioTim}',VioTxt='{tMS_Induction.VioTxt}' where Id='{tMS_Induction.Id}'", null, await GetDbTransactionAsync());
        }
        #endregion
    }
}
